package net.yxsoft.websocket.command;

import com.alibaba.fastjson.JSONObject;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.group.ChannelGroup;
import net.yxsoft.model.infrastructure.TsSession;
import net.yxsoft.model.infrastructure.TsUser;
import net.yxsoft.service.infrastructure.LoginService;
import net.yxsoft.service.infrastructure.UserService;
import net.yxsoft.websocket.MessageServer;
import net.yxsoft.websocket.MessageServerSession;
import rapid.netty.command.JsonCommand;
import rapid.netty.command.RespCommand;
import rapid.util.StrKit;

/**
 * @author wj
 */
public class SessionCmd extends JsonCommand {
  @Override
  public void execute(ChannelGroup channelGroup, ChannelHandlerContext channelHandlerContext, JSONObject jsonObject) {
    String sessionId = jsonObject.getString("sessionid");

    // 检查参数是否完整
    if(StrKit.isEmpty(sessionId)) {
      response(ctx, new RespCommand("ERROR", "参数丢失！。").toJsonString());
      return;
    }



      ChannelHandlerContext ctx = MessageServerSession.getSessionCtx(sessionId);

      if(ctx != null) {
        response(ctx, new RespCommand("LOGOUT").toJsonString());
        MessageServerSession.removeSession(sessionId);
      }

      MessageServerSession.addSession(sessionId, channelHandlerContext);




      response(channelHandlerContext, new RespCommand("SUCCESS", "1").toJsonString());



  }
}
